﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Mathematics;
using FreeGoo.GameEngine.Renderables.Links;
using FreeGoo.GameEngine.Renderables.Particles.Keys;

namespace FreeGoo.GameEngine.Renderables
{
    public class Keyhole : BodyHook
    {
        public Keyhole(BodyRenderable owningBodyRendrable, Vector2 worldPosition, float radius)
            : base(owningBodyRendrable, worldPosition, radius)
        {
        }

        public delegate void KeyAction(Key key);
        public delegate bool KeyQuery(Key key);

        public event KeyAction OnKeyInserted;
        public event KeyAction OnKeyRemoved;
        public event KeyQuery OnKeyIsAllowed;

        public override bool AllowsLinkFrom(ILinkable linkableBodyRenderable)
        {
            return 
                !IsLinked && 
                linkableBodyRenderable is Key && 
                (OnKeyIsAllowed == null || OnKeyIsAllowed(linkableBodyRenderable as Key));
        }

        public override void AddLink(Link link)
        {
            base.AddLink(link);
            if (OnKeyInserted != null)
            {
                Key key = link.OtherLinkable(this) as Key;
                OnKeyInserted(key);
            }
        }

        public override void RemoveLink(Link link)
        {
            base.RemoveLink(link);
            if (OnKeyRemoved != null)
            {
                Key key = link.OtherLinkable(this) as Key;
                OnKeyRemoved(key);
            }
        }
    }
}
